package com.ch999.detect.View.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ch999.detect.AppBaseActivity;
import com.ch999.detect.R;
import com.ch999.detect.View.adapter.JiXinNameAdatper;
import com.ch999.detect.constant.SpType;
import com.ch999.detect.mode.bean.PhoneGoodsMode;
import com.ch999.detect.utils.SharePrefUtil;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SearchJiXinActivity extends AppBaseActivity {

    public String TAG = this.getClass().getSimpleName();


    AutoCompleteTextView autotext;
    LinearLayout activitySearchJiXin;
    FrameLayout imgBack;
    TextView tvTitle;
    Button btnSearch;
    private JiXinNameAdatper mJiXinNameAdatper;

    private ArrayAdapter<String> arrayAdapter;

    private List<String> srarchParmes = new ArrayList<>();

    private Map<String, PhoneGoodsMode.DataBean.ItemsBean> mGoodsPrarms;


    @Override

    public int getLayoutId() {
        return R.layout.activity_search_ji_xin;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        autotext = (AutoCompleteTextView) findViewById(R.id.autotext);



        mGoodsPrarms = new HashMap<>();
        // mJiXinNameAdatper = new JiXinNameAdatper(mContext, mItems);

        String response = SharePrefUtil.getInstance(mContext).getString(SpType.KEY_PHONE_GOOGS);

        if (response == null) {
            return;
        }

        final Gson gson = new Gson();
        PhoneGoodsMode phoneGoodsMode = gson.fromJson(response, PhoneGoodsMode.class);

        if (phoneGoodsMode != null && phoneGoodsMode.getData().size() > 0) {

            List<PhoneGoodsMode.DataBean> goods = phoneGoodsMode.getData();


            for (PhoneGoodsMode.DataBean good : goods) {

                List<PhoneGoodsMode.DataBean.ItemsBean> itemBeans = good.getItems();
                if (itemBeans != null) {

                    for (PhoneGoodsMode.DataBean.ItemsBean itemBean : itemBeans) {

                        String pname = itemBean.getPname();
                        srarchParmes.add(pname);

                        itemBean.setBrandId(good.getBrandId());
                        mGoodsPrarms.put(pname, itemBean);

                    }

                }

            }
        }


        arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, srarchParmes);

        autotext.setAdapter(arrayAdapter);

        autotext.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Log.e(TAG, "onItemClick:===================== " + srarchParmes.get(position));
            }
        });



    }


    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.img_back) {
        } else if (i == R.id.tv_title) {
        } else if (i == R.id.btn_search) {
            String string = autotext.getText().toString();

            PhoneGoodsMode.DataBean.ItemsBean itemsBean = mGoodsPrarms.get(string);

            if (itemsBean == null || itemsBean.getPname() == null)

                Toast.makeText(mContext, "选择成功", Toast.LENGTH_SHORT).show();

            SharePrefUtil.getInstance(mContext).putString(SpType.KEY_BAND_ID, itemsBean.getBrandId() + "");
            SharePrefUtil.getInstance(mContext).putString(SpType.KEY_PHONE_PID, itemsBean.getPname() + "");

            mBaseHandler.sendEmptyMessageDelayed(0, 500);


        }
    }

    @Override
    public void findView() {

         autotext = (AutoCompleteTextView)findViewById(R.id.autotext);
         activitySearchJiXin = (LinearLayout)findViewById(R.id.activity_search_ji_xin);
         imgBack = (FrameLayout)findViewById(R.id.img_back);
         tvTitle = (TextView)findViewById(R.id.tv_title);
         btnSearch = (Button)findViewById(R.id.btn_search);
    }
}
